package goodroommate.service.impl;

import java.util.LinkedList;

import goodroommate.dal.DatabaseSession;
import goodroommate.dao.INewsDao;
import goodroommate.dao.impl.NewsDao;
import goodroommate.domain.News;
import goodroommate.exception.DatabaseException;
import goodroommate.service.INewsService;

public class NewsService implements INewsService {

	INewsDao newsDao=new NewsDao();
	@Override
	public LinkedList<News> getNews() throws DatabaseException {
		
		try(DatabaseSession dbs=new DatabaseSession()){
			
			LinkedList<News>rst=newsDao.getNews(dbs);
			return rst;
		}catch(DatabaseException e){
			e.printStackTrace();
			throw new DatabaseException(e);
		}
	}
	
	@Override
	public LinkedList<News> getAllNews() throws DatabaseException {
		
		try(DatabaseSession dbs=new DatabaseSession()){
			
			LinkedList<News>rst=newsDao.getAllNews(dbs);
			return rst;
		}catch(DatabaseException e){
			e.printStackTrace();
			throw new DatabaseException(e);
		}
	}
	@Override
	public News getNewsByID(int newsid) throws DatabaseException {
		
		if(newsid==0)
			return null;
		
		try(DatabaseSession dbs=new DatabaseSession()){
			News news=newsDao.getNewsByID(dbs, newsid);
			return news;
		}
	}
	@Override
	public boolean addNews(String subject, String content) throws DatabaseException {
		try(DatabaseSession dbs=new DatabaseSession()){
			News news=new News();
			news.setSubject(subject);
			news.setContent(content);
			return newsDao.addNews(dbs, news);
		}catch(DatabaseException e){
			e.printStackTrace();
			throw new DatabaseException(e);
		}
	}



}
